package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private HttpServletRequest request;

    @GetMapping("/list")
    public R list() {
        HttpSession session = request.getSession();
        Long userId = (Long) session.getAttribute("userId");
        R r = shoppingCartService.list(userId);
        return r;
    }

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        HttpSession session = request.getSession();
        Long userId = (Long) session.getAttribute("userId");
        shoppingCart.setUserId(userId);
        R r = shoppingCartService.add(shoppingCart);
        return r;
    }

    @DeleteMapping("/clean")
    public R clean() {
        HttpSession session = request.getSession();
        Long userId = (Long) session.getAttribute("userId");
        R r = shoppingCartService.clean(userId);
        return r;
    }

    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart) {
        HttpSession session = request.getSession();
        Long userId = (Long) session.getAttribute("userId");
        R r = shoppingCartService.sub(shoppingCart,userId);
        return r;
    }
}
